__author__ = "Chengye zhao (chengyezhao@gmail.com)"

from product import Product
from datetime import datetime
from xml.dom import minidom
from sitelist import SiteList
from photo import Photo, saveImage

import logging

SITE_URL = "http://hangzhoutuangou.appspot.com/"
cityName = u'\u676d\u5dde'
countryName = u'\u5168\u56fd'
category_food = u'\u9910\u996e\u7f8e\u98df'
category_fun = u'\u4f11\u95f2\u5a31\u4e50'
category_buy = u'\u7cbe\u54c1\u8d2d\u7269'
category_other = u'\u5176\u5b83'

def fromTimestampToString(str):
	return datetime.fromtimestamp(float(str)+8*3600)

def getText(nodelist):
	rc = []
	for node in nodelist:
		if node.nodeType == node.TEXT_NODE or node.nodeType == 4 :
			rc.append(node.data)
	return ''.join(rc)

def getTextNodeText(parent, tag):
	if len(parent.getElementsByTagName(tag)) > 0:
		return getText(parent.getElementsByTagName(tag)[0].childNodes)
	return ''

def getTextNodeDateTime(parent, tag):
	try:
		return fromTimestampToString(getTextNodeText(parent,tag))
	except TypeError:
		return None
	except ValueError:
		return None

def getTextNodeDateTime2(parent, tag):
	try:
		s = getTextNodeText(parent,tag)
		return datetime(int(s[0:4]),int(s[5:7]),int(s[8:10]))
	except TypeError:
		return None
	except ValueError:
		return None

def getTextNodeFloat(parent, tag):
	try:
		return float(getTextNodeText(parent,tag).replace(',',''))
	except TypeError:
		return None
	except ValueError:
		return None

def getTextNodeInt(parent, tag):
	try:
		return int(getTextNodeText(parent, tag).replace(',',''))
	except TypeError:
		return None
	except ValueError:
		return None

def getTextNodeCategory(parent, tag):
	try:
		s = getTextNodeText(parent, tag)
		if s == category_food:
			return 1
		elif s == category_fun:
			return 2
		elif s == category_buy:
			return 3
		elif s == category_other:
			return 4
		return None
	except TypeError:
		return None
	except ValueError:
		return None

def checkProduct(product):
	if product.site == None or product.site == '':
		return False
	if product.url == None or product.url == '':
		return False
	if product.city == None or (product.city != cityName and product.city != countryName):
		return False
	if product.title == None or product.title == '':
		return False
	return True


def ParseFunc_nuomi(site, doc):
	productList = []
	dom = minidom.parseString(doc)
	urlset = dom.getElementsByTagName("urlset")[0]
	urllist = urlset.getElementsByTagName("url")
	for url in urllist:
		data = url.getElementsByTagName("data")[0]
		display = data.getElementsByTagName("display")[0]
		#get product information
		product = Product()
		product.site = site
		product.url = getTextNodeText(url, "loc")
		product.city = getTextNodeText(display, "city")
		if product.city != cityName and product.city != countryName:
			continue	
		product.siteName = getTextNodeText(display, "website")
		product.siteurl = SiteList[site][0] 
		product.title = getTextNodeText(display,"title")
		product.detail = getTextNodeText(display,"detail")
		product.imageBig = getTextNodeText(display,"image")
		if saveImage(product.imageBig):
			product.imageMedium = SITE_URL + "image?url=" + product.imageBig + "&size=medium"
			product.imageSmall = SITE_URL + "image?url=" + product.imageBig + "&size=small"
		else:
			product.imageMedium = SITE_URL + "static/medium.png"
			product.imageSmall = SITE_URL + "static/small.png"
		product.startTime = getTextNodeDateTime(display,"startTime")
		product.endTime = getTextNodeDateTime(display,"endTime")
		product.price = getTextNodeFloat(display,"price")
		product.originalPrice = getTextNodeFloat(display,"value")
		product.rebate = getTextNodeFloat(display,"rebate")
		if product.rebate != None:
			if product.originalPrice == 0.0:
				product.rebate = 0.0
			else:
				product.rebate = product.price / product.originalPrice * 10.0
		product.sold = getTextNodeInt(display,"bought")
		product.category = getTextNodeInt(display,"category")
		if product.site == 'vctuan.com':
			product.category = 3
		if not product.category:
			product.category = getTextNodeCategory(display,"class")
		elif product.category > 3:
			product.category = None
		product.lat = getTextNodeText(display,"lat")
		product.lng = getTextNodeText(display,"lng")
		product.address = getTextNodeText(display,"address")
		product.phone = SiteList[site][2]
		if checkProduct(product):
			productList.append(product)
	return productList

def ParseFunc_58(site, doc):
	productList = []
	dom = minidom.parseString(doc)
	data  = dom.getElementsByTagName("data")[0]
	city = data.getElementsByTagName("city")[0]
	urllist = city.getElementsByTagName("product")
	for url in urllist:
		#get product information
		product = Product()
		product.site = site
		product.url = getTextNodeText(url, "url")
		product.city = getTextNodeText(city, "name")
		if product.city != cityName and product.city != countryName:
			continue	
		product.siteName = u'58\u56e2\u8d2d' 
		product.siteurl = SiteList[site][0] 
		product.phone = SiteList[site][2]
		product.title = getTextNodeText(url,"name")
		product.imageBig = getTextNodeText(url,"image")
		if saveImage(product.imageBig):
			product.imageMedium = SITE_URL + "image?url=" + product.imageBig + "&size=medium"
			product.imageSmall = SITE_URL + "image?url=" + product.imageBig + "&size=small"
		else:
			product.imageMedium = SITE_URL + "static/medium.png"
			product.imageSmall = SITE_URL + "static/small.png"
		product.startTime = getTextNodeDateTime2(url,"begin_date")
		product.endTime = getTextNodeDateTime2(url,"end_date")
		product.price = getTextNodeFloat(url,"group_price")
		product.originalPrice = getTextNodeFloat(url,"market_price")
		product.rebate = getTextNodeFloat(url,"discount")
		product.sold = getTextNodeInt(url,"quantity_sold")
		product.address = getTextNodeText(url,"region")
		if checkProduct(product):
			productList.append(product)
	return productList

def ParseFunc_meituan(site, doc):
	productList = []
	dom = minidom.parseString(doc)
	response = dom.getElementsByTagName("response")[0]
	urlset = response.getElementsByTagName("deals")[0]
	urllist = urlset.getElementsByTagName("deal")
	for url in urllist:
		#get product information
		product = Product()
		product.site = site
		product.url = getTextNodeText(url, "deal_url")
		product.city = getTextNodeText(url, "division_name")
		if product.city != cityName and product.city != countryName:
			continue	
		product.siteName = u'\u7f8e\u56e2\u7f51' 
		product.siteurl = SiteList[site][0]
		product.phone = SiteList[site][2]
		product.title = getTextNodeText(url,"title")
		product.imageBig = getTextNodeText(url,"large_image_url")
		saveImage(product.imageBig)
		if saveImage(product.imageBig):
			product.imageMedium = SITE_URL + "image?url=" + product.imageBig + "&size=medium"
			product.imageSmall = SITE_URL + "image?url=" + product.imageBig + "&size=small"
		else:
			product.imageMedium = SITE_URL + "static/medium.png"
			product.imageSmall = SITE_URL + "static/small.png"
		#product.imageMedium = getTextNodeText(url,"medium_image_url")
		#product.imageSmall = getTextNodeText(url,"small_image_url")
		product.startTime = datetime.strptime(getTextNodeText(url,"start_date")[0:19], '%Y-%m-%dT%H:%M:%S')
		product.endTime = datetime.strptime(getTextNodeText(url,"end_date")[0:19], '%Y-%m-%dT%H:%M:%S')
		product.price = getTextNodeFloat(url,"price")
		product.originalPrice = getTextNodeFloat(url,"value")
		if product.originalPrice == 0.0:
			product.rebate = 0.0
		else:
			product.rebate = getTextNodeFloat(url,"price") / getTextNodeFloat(url,"value") * 10
		product.sold = getTextNodeInt(url,"quantity_sold")
		product.lat = getTextNodeText(url,"division_lat")
		product.lng = getTextNodeText(url,"division_lng")
		product.address = getTextNodeText(url,"vendor_name")
		if checkProduct(product):
			productList.append(product)
	return productList

ParseFunc_lashou = ParseFunc_nuomi #tested 
ParseFunc_24quan = ParseFunc_nuomi #tested
ParseFunc_ftuan = ParseFunc_nuomi #tested
ParseFunc_qq = ParseFunc_nuomi #tested
ParseFunc_groupon = ParseFunc_nuomi #tested
ParseFunc_didatuan = ParseFunc_nuomi #tested
ParseFunc_haotehui = ParseFunc_nuomi #tested
ParseFunc_manzuo = ParseFunc_nuomi #tested
ParseFunc_dianping = ParseFunc_nuomi #tested
ParseFunc_55tuan = ParseFunc_nuomi #tested
ParseFunc_tuan001 = ParseFunc_nuomi #tested
ParseFunc_juqi = ParseFunc_nuomi #tested
ParseFunc_vctuan = ParseFunc_nuomi #tested

ParserList = {}
ParserList['24quan.com'] = ParseFunc_24quan
ParserList['lashou.com'] = ParseFunc_lashou
ParserList['tuan.qq.com'] = ParseFunc_qq
ParserList['ftuan.com'] = ParseFunc_ftuan
ParserList['nuomi.com'] = ParseFunc_nuomi
ParserList['meituan.com'] = ParseFunc_meituan
ParserList['t.58.com'] = ParseFunc_58
ParserList['groupon.cn'] = ParseFunc_groupon
ParserList['didatuan.com'] = ParseFunc_didatuan
ParserList['haotehui.com'] = ParseFunc_haotehui
ParserList['manzuo.com'] = ParseFunc_manzuo
ParserList['dianping.com'] = ParseFunc_dianping
ParserList['55tuan.com'] = ParseFunc_55tuan
ParserList['tuan001.com'] = ParseFunc_tuan001
ParserList['juqi.com'] = ParseFunc_juqi
ParserList['vctuan.com'] = ParseFunc_vctuan
